package me.heshang.rednamedrop.Connection;

import me.heshang.rednamedrop.Command.Command;
import me.heshang.rednamedrop.RedNamedrop;
import org.bukkit.Bukkit;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Operation {
    static PreparedStatement ps = null;
    /**
     * 增加红名等级//指令
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public void add(String name,int quantity) throws SQLException, ClassNotFoundException {
        new MySql().getSql();
        String sql = "select * from " + MySql.getTabName() + " where name='"+name+"'";
        ResultSet rs = new MySql().getSt().executeQuery(sql);//数据库操作
        while (rs.next()){
            try {
                sql = "update "+MySql.getTabName()+" set lever=? where name=?";
                ps = new MySql().connection.prepareStatement(sql);
                ps.setInt(1,rs.getInt("lever")+quantity);
                ps.setString(2,name);
                int n=ps.executeUpdate();
                if (new Command().getSend()){
                    new Command().getPlayer().sendMessage(new RedNamedrop().getMessage_addprompt_success());
                }else {
                    Bukkit.getConsoleSender().sendMessage(new RedNamedrop().getMessage_addprompt_success());
                }
                ps.close();
                new MySql().connection.close();

                return;
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (new Command().getSend()){
            new Command().getPlayer().sendMessage(new RedNamedrop().getMessage_addprompt_fail());
        }else {
            Bukkit.getConsoleSender().sendMessage(new RedNamedrop().getMessage_addprompt_fail());
        }
    }
    /**
     * 减少红名等级//指令
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public void remove(String name,int quantity) throws SQLException, ClassNotFoundException {
        new MySql().getSql();
        String sql = "select * from " + MySql.getTabName() + " where name='"+name+"'";
        ResultSet rs = new MySql().getSt().executeQuery(sql);//数据库操作
        while (rs.next()){
            try {
                if (rs.getInt("lever") >= 1 && rs.getInt("lever") - quantity >= 1) {
                    sql = "update "+MySql.getTabName()+" set lever=? where name=?";
                    ps = new MySql().connection.prepareStatement(sql);
                    ps.setInt(1, rs.getInt("lever") - quantity);
                    ps.setString(2, name);
                    int n = ps.executeUpdate();
                    if (new Command().getSend()) {
                        new Command().getPlayer().sendMessage(new RedNamedrop().getMessage_deductionprompt_success());
                    } else {
                        Bukkit.getConsoleSender().sendMessage(new RedNamedrop().getMessage_deductionprompt_success());
                    }
                    ps.close();
                    new MySql().connection.close();
                    return;
                }else {
                    if (new Command().getSend()) {
                        new Command().getPlayer().sendMessage(new RedNamedrop().getMessage_deductionprompt_exceed()+rs.getInt("lever"));
                    } else {
                        Bukkit.getConsoleSender().sendMessage(new RedNamedrop().getMessage_deductionprompt_exceed()+rs.getInt("lever"));
                    }
                    return;
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (new Command().getSend()){
            new Command().getPlayer().sendMessage(new RedNamedrop().getMessage_deductionprompt_fail());
        }else {
            Bukkit.getConsoleSender().sendMessage(new RedNamedrop().getMessage_deductionprompt_fail());
        }
    }
    /**
     * 查询等级操作
     * @param name
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public int querylever(String name) throws SQLException, ClassNotFoundException {
        new MySql().getSql();
        int lever = 0;
        ResultSet rs = new MySql().getSt().executeQuery("select * from " + MySql.getTabName() + " where name='"+name+"'");
        while(rs.next()){
            lever = rs.getInt("lever");
        }
        MySql.getConnection().close();
        return lever;
    }
}
